<template>
	<view>
		<!-- 搜索框 -->
		<view class="search">
			<u-search placeholder="搜索商品名称" v-model="goodsForm.keyword" :showAction='false'
				@search="handleSearch"></u-search>
		</view>
		<!-- 顶部分类 -->
		<view class="cate">
			<view class="title">
				<navigator url="/pages/category/category" open-type="switchTab">{{title}}</navigator>
			</view>
			<view class="salesOrder">
				<view class="sale">销量</view>
			</view>
			<view class="news" @click="handleNewGoods" :style="goodsForm.news?'color: #c55ac5;':''">新品</view>
			<view class="priceOrder">
				<view class="price" @click="handlePriceSort">价格</view>
				<view>
					<u-icon name="arrow-up-fill" :color="goodsForm.priceOrder === 'asc'?'#c55a5c':'#eee'" size="5"
						style="margin-bottom: 12rpx;"></u-icon>
					<u-icon name="arrow-down-fill" :color="goodsForm.priceOrder === 'desc'?'#c55a5c':'#eee'"
						size="5"></u-icon>
				</view>
			</view>
		</view>
		<!-- 商品展示 -->
		<template v-if="goodsList.length">
			<view class="goods-show">
				<view class="goods-item" v-for="item in goodsList" :key="item.id" @click="handleGoodsDetail(item.id)">
					<view class="item-img">
						<image :src="item.image" mode=""></image>
					</view>
					<view class="text">
						<view class="name">{{item.store_name}}</view>
						<view class="detail">
							<view class="money">￥{{item.price}}</view>
							<view class="sale">已售{{item.sales+item.unit_name}}</view>
						</view>
						<view v-if="item.vip_price" class="vip">
							<text>￥{{item.vip_price}}</text>
							<image src="../../static/img/goods-detail/svip.gif" mode=""
								style="width: 60rpx; height: 25rpx;"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="tip">没有更多内容啦~</view>
		</template>
		<template v-else>
			<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" style="background-color: #fff;">
			</u-empty>
			<view class="hot-recommend">
				<u-divider text="热门推荐" textColor="#000" lineColor="#000"></u-divider>
			</view>
		</template>
	</view>
</template>

<script>
	import { product, user } from '../../api';
	export default {
		onLoad() {
			this.goodsForm.sid = uni.getStorageSync("sid");
			this.title = uni.getStorageSync("title");
			this.getGoodsList(this.goodsForm);
			this.handleRecommend();
		},
		data() {
			return {
				title: '',
				keyword: '',
				goodsList: [],
				goodsForm: {
					sid: '',
					priceOrder: 'asc',
					keyword: '',
					news: 0,
					salesOrder: 'asc'
				}
			}
		},
		methods: {
			selected(e) {
				this.active = e
			},
			async getGoodsList(form) {
				let { status, msg, data } = await product.goods_list(form);
				if (status === 200) {
					this.goodsList = data;
				}
			},
			handleGoodsDetail(id) {
				uni.setStorageSync("goods_id", id);
				uni.navigateTo({
					url: '/pages/goods_detail/goods_detail',
				})
			},
			// 按照价格排序
			handlePriceSort() {
				switch (this.goodsForm.priceOrder) {
					case 'desc':
						this.goodsForm.priceOrder = 'asc';
						this.getGoodsList(this.goodsForm);
						break;
					case 'asc':
						this.goodsForm.priceOrder = 'desc';
						this.getGoodsList(this.goodsForm);
						break;
				}
			},
			// 商品搜索
			handleSearch() {
				this.getGoodsList(this.goodsForm);
			},
			// 新品
			handleNewGoods() {
				if (this.goodsForm.news) {
					this.goodsForm.news = 0;
					this.getGoodsList(this.goodsForm);
				} else {
					this.goodsForm.news = 1;
					this.getGoodsList(this.goodsForm);
				}
			},
			// 为你推荐
			async handleRecommend() {
				let { status, msg, data } = await user.hot_product();
				if (status === 200) {
					// 返回结果为空
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search {
		padding: 10rpx 20rpx;
		background-color: #fff;
	}

	.cate {
		height: 47rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		line-height: 47rpx;
		text-align: center;
		border-bottom: 1rpx solid #eee;
		font-size: 15rpx;
		background-color: #fff;
		padding: 20rpx 0rpx;

		.title {
			color: #c55a5c;
		}

		.title,
		.priceOrder,
		.salesOrder,
		.news {
			width: 25%;
			font-size: 26rpx;
		}

		.priceOrder {
			display: flex;
			justify-content: center;
			align-items: center;

			.price {
				margin-right: 5px;
			}
		}

		.salesOrder {
			display: flex;
			justify-content: center;
			align-items: center;

			.sale {
				margin-right: 5rpx;
			}
		}
	}

	/* 商品展示 */
	.goods-show {

		.goods-item {
			margin: 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.item-img {

				image {
					width: 200rpx;
					height: 190rpx;
				}
			}

			.text {
				padding: 11rpx 9rpx 14rpx 9rpx;
				font-size: 16rpx;
				color: #222;
				border-bottom: 3rpx solid #eee;

				.name {
					width: 443rpx;
					font-size: 26rpx;
					color: #555555;
					margin-bottom: 30rpx;
					overflow: hidden;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.detail {
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					margin-bottom: 30rpx;

					.money {
						font-size: 28rpx;
						font-weight: 700;
						margin-top: 4px;
						color: #c55a5c;
					}

					.sale {
						font-size: 12px;
						color: #aaa;
						margin-top: 3px;
					}
				}

				.vip {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					text {
						font-weight: bold;
						font-size: 20rpx;
						margin-right: 20rpx;
					}
				}
			}
		}
	}

	.tip {
		text-align: center;
		margin-top: 20rpx;
	}

	.hot-recommend {
		padding: 50rpx 180rpx;
		background-color: #fff;
		text-align: center;
		margin: 0 auto;
	}
</style>